package com.cuit.blog.controller;

import com.cuit.blog.entity.Articletype;
import com.cuit.blog.service.ArticletypeService;

import jakarta.websocket.server.PathParam;

import org.hibernate.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/articletype")
public class ArticletypeController {
	@Autowired
	ArticletypeService articletypeService;
	int pageSize = 10;

	@GetMapping("/findAll")
	public String findAll(Model model) {
		Page<Articletype> typePage = articletypeService.findAllByPage(0, pageSize);
		model.addAttribute("page", typePage);
		return "articletype/list";
	}

	@GetMapping("/page")
	public String findByPage(@RequestParam("pageIndex") int pageIndex, Model model) {
		Page<Articletype> typePage = articletypeService.findAllByPage(pageIndex, pageSize);
		int pre = typePage.previousOrFirstPageable().getPageNumber();
		int next = typePage.nextOrLastPageable().getPageNumber();
		model.addAttribute("pre", pre);
		model.addAttribute("next", next);
		model.addAttribute("page", typePage);
		return "articletype/list";
	}

	@GetMapping("/pageByName")
	public String findByName(@RequestParam("type") String type, @RequestParam("pageIndex") int pageIndex, Model model) {
		Page<Articletype> typePage = articletypeService.getArticletypePageByType(type, pageIndex, pageSize);
		int pre = 0;
		int next = 0;
		if (typePage.getNumber() > 0) {
			pre = typePage.getNumber() - 1;
		} else {
			pre = typePage.getNumber();
		}
		if (typePage.getNumber() < typePage.getTotalPages() - 1) {
			next = typePage.getNumber() + 1;
		} else {
			next = typePage.getNumber();
		}

		model.addAttribute("pre", pre);
		model.addAttribute("next", next);
		model.addAttribute("page", typePage);
		return "articletype/list";
	}

	@GetMapping("/toAdd")
	public String add(Model model) {
		Articletype type = new Articletype();
		model.addAttribute("articletype", type);
		return "articletype/add";
	}

	@PostMapping("/save")
	public String save(@ModelAttribute("articletype") Articletype articletype) {
		articletypeService.add(articletype);
		return "redirect:/articletype/findAll";
	}

	@GetMapping("/toEdit")
	public String getOne(@RequestParam("id") Long id, Model model) {
		Articletype type = articletypeService.findById(id);
		model.addAttribute("articletype", type);
		return "articletype/add";
	}

	@GetMapping(path = "/delete")
	public String delete(@RequestParam long id) {
		articletypeService.deleteById(id);
		return "redirect:/articletype/findAll";
	}
}
